package com.ygxsk.carhome.listener;

import android.app.Activity;
import android.content.Context;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * 重新改造因为内存泄漏问题
 *
 * @time 17/1/14 18:23
 */
public class MianTaskManager {

    private static MianTaskManager instance;
    private final Context mContext;
    private Stack<WeakReference<Activity>> activityStack;

    private MianTaskManager(Context context) {
        this.mContext = context.getApplicationContext();
    }

    public static MianTaskManager getInstance(Context _context) {

        if (instance == null) {
            instance = new MianTaskManager(_context);
        }
        return instance;
    }

    public void pushOneActivity(WeakReference<Activity> actvity) {

        if (activityStack == null) {
            activityStack = new Stack<WeakReference<Activity>>();
        }

        activityStack.add(actvity);

//        LogUtils.d("itchentask-push->" + actvity.get().getClass().getName().toString()+"-->"+activityStack.size());
    }

    public WeakReference<Activity> getLastActivity() {

        return activityStack.lastElement();
    }


    public void popOneActivity(WeakReference<Activity> _activity) {

        if (activityStack != null && activityStack.size() > 0) {

            if (_activity != null) {

                if (_activity.get() != null) {
                    //LogUtils.d("itchentask--popremove-->" + _activity.get().getClass().getName().toString());
                    activityStack.remove(_activity);
                    _activity.get().finish();
                    _activity = null;
                }
            }

        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

	/*for (int i = 0, size = activityStack.size(); i < size; i++) {
			if (null != activityStack.get(i)) {
				activityStack.get(i).finish();
			}
		}
		activityStack.clear();*/

    public void finishAllActivity() {

        if (activityStack != null) {

            //LogUtils.d("itchentask-finishall-totalNumber=" + activityStack.size());

            if (activityStack.size() > 0) {

                //一段开源中国的代码是:
                for (int position = 0, size = activityStack.size(); position < size; position++) {

                    if (null != activityStack.get(position)) {
                        if (activityStack.get(position).get() != null) {
                            //LogUtils.d("itchentask--要删除了---"+activityStack.get(position).get().getClass().getName());
                            activityStack.get(position).get().finish();
                        }
                    }
                }
            }

            //LogUtils.d("itchentask-剩余的内容:--"+activityStack.size());
            activityStack.clear();
            //activityStack=null;
            //原始代码:
			/*while (activityStack.size() > 0) {
				WeakReference<Activity> activity = getLastActivity();
				if (activity.get() == null)
					break;
				popOneActivity(activity);
			}*/
        }

        //activityStack=null;
		/*if (instance!=null){
			instance = null;
		}*/


    }


    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (WeakReference<Activity> activityWeakReference : activityStack) {
            Activity activity = activityWeakReference.get();
            if (activity != null) {
                if (activity.getClass().equals(cls)) {
                    //LogUtils.d("itchentask--删除指定-->"+activity.getClass());
                    finishActivity(activity);
                }
            }

        }
    }


    /**2017年1月15日注释无效代码*/
	/*public WeakReference<Activity> getFirstActivity() {
		//LogUtils.d("itchen -FirstActivity=" + activityStack.firstElement().getClass().toString());
		return activityStack.firstElement();
	}*/

    //itchencopy
	/*public int getActivityCount() {

		if (activityStack == null) {
			return -1;
		}
		return activityStack.size();

	}*/
    //itchencopy
	/*public Activity currentActivity() {

		if (activityStack == null) {
			return null;
		}

		if (!activityStack.listIterator().hasNext()) {
			return null;
		}

		Activity activity = activityStack.lastElement();
		return activity;
	}*/

}
